﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Koala.Pro.Helpers
{
    public class FileDialogHelper
    {
        private static string AllFilter = "All File(*.*)|*.*";

        private static string WordFilter = "Word(*.doc)|*.doc|Word(*.docx)|*.docx|All File(*.*)|*.*";

        private static string ExcelFilter = "Excel(*.xls)|*.xls|Excel(*.xlsx)|*.xlsx|All File(*.*)|*.*";

        private static string PdfFilter = "PDF(*.pdf)|*.pdf|All File(*.*)|*.*";

        private static string ImageFilter = "Image Files(*.BMP;*.bmp;*.JPG;*.jpg;*.GIF;*.gif;*.png;*.PNG)|(*.BMP;*.bmp;*.JPG;*.jpg;*.GIF;*.gif;*.png;*.PNG)|All File(*.*)|*.*";

        private static string HtmlFilter = "HTML files (*.html;*.htm)|*.html;*.htm|All files (*.*)|*.*";

        private static string AccessFilter = "Access(*.mdb)|*.mdb|All File(*.*)|*.*";

        private static string ZipFillter = "Zip(*.zip)|*.zip|Rar(*.rar)|*.rar|All files (*.*)|*.*";

        private const string ConfigFilter = "Configuration Files(*.cfg)|*.cfg|All File(*.*)|*.*";

        private static string TxtFilter = "Text (*.txt)|*.txt|All files (*.*)|*.*";

        private static string XmlFilter = "XML Files(*.xml)|*.xml|All files (*.*)|*.*";

        private static string RarFilter = "Rar(*.rar)|*.rar|All files (*.*)|*.*";

        private static string SqliteFilter = "Sqlite Files(*.db)|*.db|All files (*.*)|*.*";

        private FileDialogHelper()
        {
        }

        public static string OpenFile()
        {
            return Open("打开文件", AllFilter);
        }

        public static string OpenFile(bool multiselect)
        {
            return OpenFile(multiselect, "");
        }

        public static string OpenFile(bool multiselect, string fileName)
        {
            return OpenFile(multiselect, fileName, null);
        }

        public static string OpenFile(bool multiselect, string fileName, string initialDirectory)
        {
            if (multiselect)
            {
                return OpenMultiselect("打开多个文件", AllFilter, fileName, initialDirectory);
            }
            return Open("打开文件", AllFilter, fileName, initialDirectory);
        }

        public static string SaveFile()
        {
            return SaveFile(string.Empty);
        }

        public static string SaveFile(string filename)
        {
            return Save("保存文件", AllFilter, filename);
        }

        public static string SaveFile(string filename, string initialDirectory)
        {
            return Save("保存文件", AllFilter, filename, initialDirectory);
        }

        public static string OpenText()
        {
            return Open("选择文本文件选择", TxtFilter);
        }

        public static string OpenText(bool multiselect)
        {
            return OpenText(multiselect, "");
        }

        public static string OpenText(bool multiselect, string fileName)
        {
            return OpenText(multiselect, fileName, null);
        }

        public static string OpenText(bool multiselect, string fileName, string initialDirectory)
        {
            if (multiselect)
            {
                return OpenMultiselect("选择多个文本文件", TxtFilter, fileName);
            }
            return Open("选择文本文件", TxtFilter, fileName);
        }

        public static string SaveText()
        {
            return SaveText(string.Empty);
        }

        public static string SaveText(string filename)
        {
            return Save("保存文本文件", TxtFilter, filename);
        }

        public static string SaveText(string filename, string initialDirectory)
        {
            return Save("保存文本文件", TxtFilter, filename, initialDirectory);
        }

        public static string OpenExcel()
        {
            return Open("选择Excel文件", ExcelFilter);
        }

        public static string OpenExcel(bool multiselect)
        {
            return OpenExcel(multiselect, "");
        }

        public static string OpenExcel(bool multiselect, string fileName)
        {
            return OpenExcel(multiselect, fileName, null);
        }

        public static string OpenExcel(bool multiselect, string fileName, string initialDirectory)
        {
            if (multiselect)
            {
                return OpenMultiselect("选择多个Excel文件", ExcelFilter, fileName, initialDirectory);
            }
            return Open("选择Excel文件", ExcelFilter, fileName, initialDirectory);
        }

        public static string SaveExcel()
        {
            return SaveExcel(string.Empty);
        }

        public static string SaveExcel(string filename)
        {
            return Save("保存Excel", ExcelFilter, filename);
        }

        public static string SaveExcel(string filename, string initialDirectory)
        {
            return Save("保存Excel", ExcelFilter, filename, initialDirectory);
        }

        public static string OpenWord()
        {
            return Open("选择Word文件", WordFilter);
        }

        public static string OpenWord(bool multiselect)
        {
            return OpenWord(multiselect, "");
        }

        public static string OpenWord(bool multiselect, string fileName)
        {
            return OpenWord(multiselect, fileName, null);
        }

        public static string OpenWord(bool multiselect, string fileName, string initialDirectory)
        {
            if (multiselect)
            {
                return OpenMultiselect("选择多个Word文件", WordFilter, fileName, initialDirectory);
            }
            return Open("选择Word文件", WordFilter, fileName, initialDirectory);
        }

        public static string SaveWord()
        {
            return SaveWord(string.Empty);
        }

        public static string SaveWord(string filename)
        {
            return Save("保存Word", WordFilter, filename);
        }

        public static string SaveWord(string filename, string initialDirectory)
        {
            return Save("保存Word", WordFilter, filename, initialDirectory);
        }

        public static string OpenPdf()
        {
            return Open("选择PDF文件", PdfFilter);
        }

        public static string OpenPdf(bool multiselect)
        {
            return OpenPdf(multiselect, "");
        }

        public static string OpenPdf(bool multiselect, string fileName)
        {
            return OpenPdf(multiselect, fileName, null);
        }

        public static string OpenPdf(bool multiselect, string fileName, string initialDirectory)
        {
            if (multiselect)
            {
                return OpenMultiselect("选择多个PDF文件", PdfFilter, fileName, initialDirectory);
            }
            return Open("选择PDF文件", PdfFilter, fileName, initialDirectory);
        }

        public static string SavePdf()
        {
            return SavePdf(string.Empty);
        }

        public static string SavePdf(string filename)
        {
            return Save("保存PDF", PdfFilter, filename);
        }

        public static string SavePdf(string filename, string initialDirectory)
        {
            return Save("保存PDF", PdfFilter, filename, initialDirectory);
        }

        public static string OpenHtml()
        {
            return Open("选择Html文件", HtmlFilter);
        }

        public static string OpenHtml(bool multiselect)
        {
            if (multiselect)
            {
                return OpenMultiselect("选择多个Html文件", HtmlFilter, "");
            }
            return Open("选择Html文件", HtmlFilter);
        }

        public static string SaveHtml()
        {
            return SaveHtml(string.Empty);
        }

        public static string SaveHtml(string filename)
        {
            return Save("保存Html", HtmlFilter, filename);
        }

        public static string SaveHtml(string filename, string initialDirectory)
        {
            return Save("保存Html", HtmlFilter, filename, initialDirectory);
        }

        public static string OpenZip()
        {
            return Open("选择压缩文件", ZipFillter);
        }

        public static string OpenZip(string filename)
        {
            return Open("选择压缩文件", ZipFillter, filename);
        }

        public static string OpenZip(bool multiselect)
        {
            return OpenZip(multiselect, "");
        }

        public static string OpenZip(bool multiselect, string fileName)
        {
            return OpenZip(multiselect, fileName, null);
        }

        public static string OpenZip(bool multiselect, string fileName, string initialDirectory)
        {
            if (multiselect)
            {
                return OpenMultiselect("选择多个压缩文件", ZipFillter, fileName, initialDirectory);
            }
            return Open("选择压缩文件", ZipFillter, fileName, initialDirectory);
        }

        public static string SaveZip()
        {
            return SaveZip(string.Empty);
        }

        public static string SaveZip(string filename)
        {
            return Save("保存压缩文件", ZipFillter, filename);
        }

        public static string SaveZip(string filename, string initialDirectory)
        {
            return Save("保存压缩文件", ZipFillter, filename, initialDirectory);
        }

        public static string OpenRar()
        {
            return Open("选择RAR压缩文件", RarFilter);
        }

        public static string OpenRar(string filename)
        {
            return Open("选择RAR压缩文件", RarFilter, filename);
        }

        public static string OpenRar(bool multiselect)
        {
            return OpenRar(multiselect, "");
        }

        public static string OpenRar(bool multiselect, string fileName)
        {
            return OpenRar(multiselect, fileName, null);
        }

        public static string OpenRar(bool multiselect, string fileName, string initialDirectory)
        {
            if (multiselect)
            {
                return OpenMultiselect("选择多个压缩文件", RarFilter, fileName, initialDirectory);
            }
            return Open("选择压缩文件", RarFilter, fileName, initialDirectory);
        }

        public static string SaveRar()
        {
            return SaveRar(string.Empty);
        }

        public static string SaveRar(string filename)
        {
            return Save("保存压缩文件", RarFilter, filename);
        }

        public static string SaveRar(string filename, string initialDirectory)
        {
            return Save("保存RAR压缩文件", RarFilter, filename, initialDirectory);
        }

        public static string OpenSqlite()
        {
            return Open("选择Sqlite数据库文件", SqliteFilter);
        }

        public static string OpenSqlite(string filename)
        {
            return Open("选择Sqlite数据库文件", SqliteFilter, filename);
        }

        public static string OpenSqlite(bool multiselect)
        {
            return OpenSqlite(multiselect, "");
        }

        public static string OpenSqlite(bool multiselect, string fileName)
        {
            return OpenSqlite(multiselect, fileName, null);
        }

        public static string OpenSqlite(bool multiselect, string fileName, string initialDirectory)
        {
            if (multiselect)
            {
                return OpenMultiselect("选择多个Sqlite数据库文件", SqliteFilter, fileName, initialDirectory);
            }
            return Open("选择Sqlite数据库文件", SqliteFilter, fileName, initialDirectory);
        }

        public static string SaveSqlite()
        {
            return SaveSqlite(string.Empty);
        }

        public static string SaveSqlite(string filename)
        {
            return Save("保存Sqlite数据库文件", SqliteFilter, filename);
        }

        public static string SaveSqlite(string filename, string initialDirectory)
        {
            return Save("保存Sqlite数据库文件", SqliteFilter, filename, initialDirectory);
        }

        public static string OpenXml()
        {
            return Open("选择Xml文件", XmlFilter);
        }

        public static string OpenXml(string filename)
        {
            return Open("选择Xml文件", XmlFilter, filename);
        }

        public static string OpenXml(bool multiselect)
        {
            return OpenXml(multiselect, "");
        }

        public static string OpenXml(bool multiselect, string fileName)
        {
            return OpenXml(multiselect, fileName, null);
        }

        public static string OpenXml(bool multiselect, string fileName, string initialDirectory)
        {
            if (multiselect)
            {
                return OpenMultiselect("选择多个Xml文件", XmlFilter, fileName, initialDirectory);
            }
            return Open("选择Xml文件", XmlFilter, fileName, initialDirectory);
        }

        public static string SaveXml()
        {
            return SaveXml(string.Empty);
        }

        public static string SaveXml(string filename)
        {
            return Save("保存Xml文件", XmlFilter, filename);
        }

        public static string SaveXml(string filename, string initialDirectory)
        {
            return Save("保存Xml文件", XmlFilter, filename, initialDirectory);
        }

        public static string OpenImage()
        {
            return Open("选择图片文件", ImageFilter);
        }

        public static string OpenImage(bool multiselect)
        {
            return OpenImage(multiselect, "");
        }

        public static string OpenImage(bool multiselect, string fileName)
        {
            return OpenImage(multiselect, fileName, null);
        }

        public static string OpenImage(bool multiselect, string fileName, string initialDirectory)
        {
            if (multiselect)
            {
                return OpenMultiselect("选择多个图片文件", ImageFilter, fileName, initialDirectory);
            }
            return Open("选择图片文件", ImageFilter, fileName, initialDirectory);
        }

        public static string SaveImage()
        {
            return SaveImage(string.Empty);
        }

        public static string SaveImage(string filename)
        {
            return Save("保存图片", ImageFilter, filename);
        }

        public static string SaveImage(string filename, string initialDirectory)
        {
            return Save("保存图片", ImageFilter, filename, initialDirectory);
        }

        public static string SaveAccessDb()
        {
            return Save("数据库备份", AccessFilter);
        }

        public static string SaveBakDb()
        {
            return Save("数据库备份", "Access(*.bak)|*.bak");
        }

        public static string OpenBakDb(string file)
        {
            return Open("数据库还原", "Access(*.bak)|*.bak", file);
        }

        public static string OpenAccessDb()
        {
            return Open("数据库还原", AccessFilter);
        }

        public static string SaveConfig()
        {
            return Save("配置文件备份", "Configuration Files(*.cfg)|*.cfg|All File(*.*)|*.*");
        }

        public static string OpenConfig()
        {
            return Open("配置文件还原", "Configuration Files(*.cfg)|*.cfg|All File(*.*)|*.*");
        }

        public static string OpenDir()
        {
            return OpenDir(string.Empty);
        }

        public static string OpenDir(string selectedPath)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.Description = JsonLanguage.Default.GetString("请选择路径");
            folderBrowserDialog.SelectedPath = selectedPath;
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                return folderBrowserDialog.SelectedPath;
            }
            return string.Empty;
        }

        public static string OpenMultiselect(string title, string filter, string filename)
        {
            return OpenMultiselect(title, filter, filename, null);
        }

        public static string OpenMultiselect(string title, string filter, string filename, string initialDirectory)
        {
            title = JsonLanguage.Default.GetString(title);
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = filter;
            openFileDialog.Title = title;
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FileName = filename;
            openFileDialog.Multiselect = true;
            if (!string.IsNullOrEmpty(initialDirectory))
            {
                openFileDialog.InitialDirectory = initialDirectory;
            }
            string text = "";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string[] fileNames = openFileDialog.FileNames;
                foreach (string arg in fileNames)
                {
                    text += $"{arg},";
                }
            }
            return text.Trim(',');
        }

        public static string Open(string title, string filter, string filename)
        {
            return Open(title, filter, filename, null);
        }

        public static string Open(string title, string filter, string filename, string initialDirectory)
        {
            title = JsonLanguage.Default.GetString(title);
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = filter;
            openFileDialog.Title = title;
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FileName = filename;
            if (!string.IsNullOrEmpty(initialDirectory))
            {
                openFileDialog.InitialDirectory = initialDirectory;
            }
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                return openFileDialog.FileName;
            }
            return string.Empty;
        }

        public static string Open(string title, string filter)
        {
            return Open(title, filter, string.Empty);
        }

        public static string Save(string title, string filter, string filename)
        {
            return Save(title, filter, filename, "");
        }

        public static string Save(string title, string filter, string filename, string initialDirectory)
        {
            title = JsonLanguage.Default.GetString(title);
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = filter;
            saveFileDialog.Title = title;
            saveFileDialog.FileName = filename;
            saveFileDialog.RestoreDirectory = true;
            if (!string.IsNullOrEmpty(initialDirectory))
            {
                saveFileDialog.InitialDirectory = initialDirectory;
            }
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                return saveFileDialog.FileName;
            }
            return string.Empty;
        }

        public static string Save(string title, string filter)
        {
            return Save(title, filter, string.Empty);
        }

        public static Color PickColor()
        {
            Color result = SystemColors.Control;
            ColorDialog colorDialog = new ColorDialog();
            if (DialogResult.OK == colorDialog.ShowDialog())
            {
                result = colorDialog.Color;
            }
            return result;
        }

        public static Color PickColor(Color color)
        {
            Color result = SystemColors.Control;
            ColorDialog colorDialog = new ColorDialog();
            colorDialog.Color = color;
            if (DialogResult.OK == colorDialog.ShowDialog())
            {
                result = colorDialog.Color;
            }
            return result;
        }
    }
}
